<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

require_once ('api/IDataAccessObject.php');

/**
 *
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel_DAO
 */
abstract class DAO implements IDataAccessObject
{

  public static $defaultEngine = 'MDB2';

  /**
   *
   * @var array
   */
  private static $instances = array();

  /**
   *
   * @var IDictionary
   */
  protected $params;

  /**
   *
   * @param mixed $params
   * @return void
   */
  protected function __construct($params)
  {
    $this->params = SimpleDictionary::parseFrom($params);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IDataAccessObject#query($sql)
   */
  public function query($sql = null)
  {
    if (is_a($sql, 'IQueryObject')) {
      return $sql;
    }

    return $this->factoryQueryObject()->sql($sql);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IDataAccessObject#query($sql)
   */
  public function insert($table = null, $data = null)
  {
    return $this->factoryQueryObject()->insert($table, $data);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IDataAccessObject#query($sql)
   */
  public function update($table = null, $data = null, $where = null)
  {
    return $this->factoryQueryObject()->update($table, $data, $where);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IDataAccessObject#query($sql)
   */
  public function delete($table = null, $where = null)
  {
    return $this->factoryQueryObject()->delete($table, $where);
  }

  /**
   *
   * @param string $id
   * @param string $engine
   * @param mixed $params
   * @return IDataAccessObject
   */
  public static function getInstance($id = 'default', $engine = null, $params = null)
  {
    if (! isset(DAO::$instances[$id])) {
      if (empty($engine)) {
        $engine = DAO::$defaultEngine;
      }

      $class = $engine . '_DAO';

      if (! Simplify::getConfig()->has('database:' . $id)) {
        throw new Exception("Database configuration for <b>$id</b> not found");
      }

      $server = $_SERVER['SERVER_NAME'];

      if ($id == 'default' && Simplify::getConfig()->has('database:' . $server)) {
        $_params = Simplify::getConfig()->get('database:default');
        $_params->copyAll(Simplify::getConfig()->get('database:' . $server));
      }
      else {
        $_params = Simplify::getConfig()->get('database:' . $id);
      }

      $_params->copyAll($params);

      $dao = new $class($_params);

      DAO::$instances[$id] = $dao;
    }

    return DAO::$instances[$id];
  }

  public static function log($data = null)
  {
    static $log;

    if (empty($log)) {
      $log = array();
    }

    if (! empty($data)) {
      $log[] = array_filter($data);
    }

    return $log;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/dao/api/IDataAccessObject#quote($value, $type)
   */
  public function quote($value, $type = null)
  {
    return $value;
  }

}

?>